const app = getApp();
const {upTime} = require('/util/public');
Page({
    data: {
        config: app.config,

        query:'',
        list:[],
        productOrderRecordVoList:[],

        buyBreakSta: false, //是否是归还订单

        surplusNum: 0,
        thisMonthIndex: 0,
        oid: '',

        priceAll: 0,
        selctStaAll: false,
        checkIdsArr: []
    },
    onLoad(query) {
        let id = JSON.parse(query.param);
        this.setData({
          query,
          oid: id
        });
        this.getoOrderBill(id);
    },

    // 全部
    selectAllBtn(){
      let arr = this.data.list;
      let sta = true;
      if(this.data.selctStaAll){
        sta = false
      }else{
        sta = true
      }

      arr.forEach(item=>{
        item.selctSta = sta
      })
      this.setData({
        list: arr,
        selctStaAll: sta
      })
      this.calculateFun();
    },


    // 勾选
    selectBtn(e){
      console.log(e)
      let dat = e.target.dataset.item.item;
      let index = e.target.dataset.item.index;
      let arr = this.data.list;

      let sta = dat.selctSta;
      dat.selctSta = !sta;

      arr[index] = dat;

      this.setData({
        list: arr
      })
      this.calculateFun();
    },


    // 计算金额
    calculateFun(){
      let arr = this.data.list;
      let checkArr = [];
      let num = 0;
      arr.forEach(item=>{
        if(item.selctSta){
          console.log(item.price)
          checkArr.push(item.id);
          num += item.price-0;
        }
      })
      

      num = num.toFixed(2);

      this.setData({
        checkIdsArr: checkArr,
        priceAll: num
      })

    },





    navigateTo(e){
      app.navigateTo(e);
    },

    // 获取账单数据
    getoOrderBill(id){
        const time = new Date().setHours(0,0,0,0)/1000+(24*60*60);
        console.log(888999,(time));

        app.order.detail(id)
        .then(res=>{
            console.log(res)
            let productOrderRecordVoList = res.data.productOrderRecordVoList;

            let priceFalseArr = [];

            productOrderRecordVoList.forEach((item,index)=>{
                
                item.time = upTime("%Y年%M月%D日",time);
                item.createtime = upTime("%Y年%M月%D日",item.createtime);
                item.duetime = upTime("%Y年%M月%D日",item.duetime);
                item.expiretime = upTime("%Y年%M月%D日",item.expiretime);
                item.price = (item.price/100).toFixed(2);
                item.selctSta = false;

                if(!item.payment){
                  priceFalseArr.push(item)
                }
                
            })
            console.log(567,priceFalseArr)


            this.setData({
                list:priceFalseArr,
                productOrderRecordVoList
            })
        })
    },



    // 支付账单
    async onpay(){

        const oid = this.data.oid,
        id = this.data.checkIdsArr;
        let muchId = id.join(',');
        

        if(id.length==0){
          my.showToast({
            content:'请先选择需要还款的期数账单'
          })
          return;
        }

        let dat = {
          oid: oid,
          orIds: muchId
        }

        if(id.length == 1){
          const res = await app.order.alipay(oid,id[0]);
          console.log(333,res);
          const pay = await app.alipay(res.data);
          console.log(555,pay);
          if(pay.resultCode == 9000) {
            my.alert({
              content: "支付成功",
            });
            setTimeout(() => {
              my.navigateBack({ delta: 1 })
            }, 500)
          };
        }else{
          const res = await app.order.alipayRecords(dat);
          console.log(333,res);
          const pay = await app.alipay(res.data);
          console.log(555,pay);
          if(pay.resultCode == 9000) {
              my.alert({
                content: "支付成功",
              });
              setTimeout(() => {
                my.navigateBack({ delta: 1 })
              }, 500)
          };
        }

        

        // const res = await app.order.alipayRecords(dat);
        // console.log(333,res);
        // const pay = await app.alipay(res.data);
        // console.log(555,pay);
        // if(pay.resultCode == 9000) {
        //     let productOrderRecordVoList = this.data.productOrderRecordVoList;
        // };
    },

});
